package com.example.j280_webproject.servlet;

import com.example.j280_webproject.day036.JDBC02_Login;
import com.example.j280_webproject.day036.JDBC02_Login2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        // 不要每一次请求都连接数据库，如果能够通过代码直接验证判断的，那么不需要使用数据库连接，这样可以提升一定量的性能
        if(username == null || password == null ||
                "".equals(username) || "".equals(password)) {     // 1. 验证用户是否输入了合法用户名和密码
            writer.print("请输入用户名或者密码");
            return;
        }

        JDBC02_Login2 jdbc02_login2 = new JDBC02_Login2();
        try {
            boolean b = jdbc02_login2.checkLogin(username, password);
            String s = b ? "success" : "error";

            writer.print(s);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            writer.print("登录失败! 系统繁忙，请稍候重试!");
        }
    }
}
